<?php

namespace Portal\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

use Portal\UserBundle\Entity\User;

class IndexController extends Controller
{
    public function testAction()
    {
      $user = $this->container->get('security.context')->getToken()->getUser();

      if ($user && !is_object($user)) {
        $name = 'Guest';
      } else {
        $name = $user->getUsername();
      }
      $request = $this->getRequest();

      return $this->render('PortalMainBundle:Index:test.html.twig', array(
                  'name' => $name
              ));
    }
    
    public function indexAction()
    { 
      $user = $this->container->get('security.context')->getToken()->getUser();

      if ($user && !is_object($user)) {
        $name = 'Guest';        
      }else{
        $name = $user->getUsername();
      }
      $request = $this->getRequest();
    
      return $this->render('PortalMainBundle:Index:index.html.twig', array(
          'name' => $name,
          'sliders' => $this->getMainSlides()
      ));
    }
    
    public function topMenuAction()
    {
      $em = $this->getDoctrine()->getManager();

      $menuList = $em->getRepository('PortalMainBundle:Main')->getAllParentMenuByStatus($this->container->getParameter('statusActive'));
      
      for ($i = 0; $i < count($menuList); $i++) {
        $tmpParentId = $menuList[$i]['id'];
        $child = $em->getRepository('PortalMainBundle:Main')->getChildByParentId($tmpParentId, $this->container->getParameter('statusActive'));
        if ($child) {
          $childIndex = 0;
          foreach ($child AS $childItem) {
            $menuList[$i]['child'][$childIndex] = $childItem;
            $childIndex++;
          }
        } else {
          $menuList[$i]['child'] = null;
        }
      }
      return $this->render('PortalMainBundle:Menu:menu.html.twig', array(
                'menuList' => $menuList,
            ));
    }
    
    
    public function changeLocaleToEnAction()
    {
      var_dump($this->get('session')->get('_locale'));
      $request = $this->getRequest();
      $request->setDefaultLocale('en');
      $this->get('session')->set('_locale', 'en');
      var_dump($this->get('session')->get('_default_locale'));die;
      return $this->redirect($this->generateUrl("test"));
    }
    
    public function changeLocaleToMnAction()
    {
      var_dump($this->get('session')->get('_default_locale'));
      $request = $this->getRequest();
      $request->setDefaultLocale('mn');      
      $this->get('session')->set('_locale', 'mn');
      var_dump($this->get('session')->get('_default_locale'));
      return $this->redirect($this->generateUrl("test"));
      
    }
    
    public function myProfileAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();
        var_dump($user);
        var_dump($this->container->get('security.context')->getToken()->getRoles());
        
        if(TRUE ===$this->get('security.context')->isGranted('ROLE_TUTOR') )
        {
            return $this->redirect(
                            $this->generateUrl("tutorProfile", array())
            );
            
        }
        else{
            return $this->redirect(
                            $this->generateUrl("step6", array())
            );
        }
        
    }
    
    private function getMainSlides()
    {
        $mainSlides = $this->getDoctrine()->getManager()->getRepository('PortalMainBundle:Menu')->getMainSlides();
        return $mainSlides;
    }
    
}
